#include "VSShaderStringFactory.h"
#include "VSGeometry.h"
#include "VSLog.h"
#include "VSResourceManager.h"
#include "VSBoneNode.h"
#include "VSDirectionLight.h"
#include "VSPointLight.h"
#include "VSSpotLight.h"
#include "VSProjLight.h"
#include "VSGraphicInclude.h"
using namespace VSEngine2;
VSString VSShaderStringFactory::ms_PSTextureInputCoordValue[TEXLEVEL];
VSString VSShaderStringFactory::ms_PSColor[2];
VSString VSShaderStringFactory::ms_PSTextureOutputColorValue;
VSString VSShaderStringFactory::ms_PSInputLocalNormal;
VSString VSShaderStringFactory::ms_PSInputLocalBinormal;
VSString VSShaderStringFactory::ms_PSInputLocalTangent;
VSString VSShaderStringFactory::ms_PSInputLocalPos;
VSString VSShaderStringFactory::ms_PSProjLightSampler;
VSString VSShaderStringFactory::ms_PSOrenNayarLookUpTableSampler;
unsigned int VSShaderStringFactory::ms_uiCreateVShaderNum = 0;
unsigned int VSShaderStringFactory::ms_uiCreatePShaderNum = 0;
VSString VSShaderStringFactory::ms_WorldViewProjectMatrix;
VSString VSShaderStringFactory::ms_WorldViewMatrix;
VSString VSShaderStringFactory::ms_BoneMatrix;
VSString VSShaderStringFactory::ms_WorldMatrix;
VSString VSShaderStringFactory::ms_CameraWorldPos;
VSString VSShaderStringFactory::ms_LightName[VSLight::LT_MAX];
VSString VSShaderStringFactory::ms_FarZ;
VSString VSShaderStringFactory::ms_PSInputViewZ;
VSString VSShaderStringFactory::ms_WorldNormal;
VSString VSShaderStringFactory::ms_WorldPos;
VSString VSShaderStringFactory::ms_WorldCameraDir;
void VSShaderStringFactory::ClearAllString()
{
	for(unsigned int i = 0 ; i < TEXLEVEL ; i++)
		ms_PSTextureInputCoordValue[i] = VSRenderer::ms_pRenderer->Float2Const(_T("-1"),_T("-1"));
	ms_PSTextureOutputColorValue.Clear();
	ms_PSInputLocalNormal.Clear();
	ms_PSInputLocalBinormal.Clear();
	ms_PSInputLocalTangent.Clear();
	ms_PSInputLocalPos.Clear();
	ms_PSProjLightSampler.Clear();
	ms_PSColor[0].Clear();
	ms_PSColor[1].Clear();

	ms_WorldViewProjectMatrix.Clear();
	ms_WorldViewMatrix.Clear();
	ms_BoneMatrix.Clear();
	ms_WorldMatrix.Clear();
	ms_CameraWorldPos.Clear();

	ms_WorldNormal.Clear();
	ms_WorldPos.Clear();

	ms_FarZ.Clear();
	ms_PSInputViewZ.Clear();
	ms_PSOrenNayarLookUpTableSampler.Clear();
	ms_PSProjLightSampler.Clear();
	ms_WorldCameraDir.Clear();
	for(unsigned int i = 0 ; i < VSLight::LT_MAX ; i++)
		ms_LightName[VSLight::LT_MAX].Clear();
}

bool VSShaderStringFactory::CreateVShaderString(VSVShader * pVShader,MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID,VSString & VShaderString
#ifdef _DEBUG
												,VSString &OutShaderFileName)
#else
												)
#endif
{
	if (!MSPara1.pGeometry || !pVShader || !VSRenderer::ms_pRenderer || !MSPara1.pMaterialInstance)
	{
		return 0;
	}
	VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();
	if (!pMaterial)
	{
		return 0;
	}
	ClearAllString();
	ms_uiCreateVShaderNum++;

	VSString VInclude;
	VSString VDynamic;
	VSString VInputDeclare;
	VSString VOutputDeclare;
	VSString VUserConstantString;
	VSString VFuntionString;
	VSLog VShaderText;
	VSString VShaderTextName;
	VSString ShaderID = IntToString(uiShaderID);
	VSRenderer::ms_pRenderer->GetIncludeShader(VInclude);
	VSRenderer::ms_pRenderer->GetDynamicShader(VDynamic);
	VSRenderer::ms_pRenderer->CreateVUserConstant(pVShader,MSPara1,uiPassType,VUserConstantString);
	VSRenderer::ms_pRenderer->CreateVInputDeclare(MSPara1,uiPassType,VInputDeclare);
	VSRenderer::ms_pRenderer->CreateVOutputDeclare(MSPara1,uiPassType,VOutputDeclare);
	VSRenderer::ms_pRenderer->CreateVFuntion(MSPara1,uiPassType,VFuntionString);
	if (uiPassType == VSPass::PT_MATERIAL)
	{			
		
		VShaderTextName = VSResourceManager::ms_OutputShaderCodePath + pMaterial->GetShowName().GetString() + ShaderID + _T("VShader.txt");
		
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{
				
		VShaderTextName = VSResourceManager::ms_OutputShaderCodePath + pMaterial->GetShowName().GetString() + _T("_NormalDeph") + ShaderID + _T("VShader.txt");

	}
	else if (uiPassType == VSPass::PT_PREZ)
	{

		VShaderTextName = VSResourceManager::ms_OutputShaderCodePath + pMaterial->GetShowName().GetString() + _T("_PreZ") + ShaderID + _T("VShader.txt");

	}
	VShaderText.Open(VShaderTextName.GetBuffer());
	VShaderString = VInclude + VDynamic + VUserConstantString + VInputDeclare + VOutputDeclare + VFuntionString;
	VShaderText.WriteInfo(VShaderString.GetBuffer());
#ifdef _DEBUG
	OutShaderFileName = VShaderTextName;
#endif
	return 1;
}
bool VSShaderStringFactory::CreatePShaderString(VSPShader * pPShader,MaterialShaderPara1 &MSPara1,unsigned int uiPassType,unsigned int uiShaderID,VSString & PShaderString
#ifdef _DEBUG
												,VSString &OutShaderFileName)
#else
												)
#endif
{
	if (!pPShader || !MSPara1.pGeometry || !MSPara1.pMaterialInstance || !VSRenderer::ms_pRenderer)
	{
		return 0;
	}
	VSMaterial * pMaterial = MSPara1.pMaterialInstance->GetMaterial();

	ClearAllString();
	ms_uiCreatePShaderNum++;
	VSString PInclude;
	VSString PDynamic;
	VSString PInputDeclare;
	VSString POutputDeclare;
	VSString PUserConstantstring;
	VSString PFuntionString;
	VSLog PShaderText;
	VSString PShaderTextName;
	VSString ShaderID = IntToString(uiShaderID);
	VSRenderer::ms_pRenderer->GetIncludeShader(PInclude);
	VSRenderer::ms_pRenderer->GetDynamicShader(PDynamic);
	VSRenderer::ms_pRenderer->CreatePUserConstant(pPShader,MSPara1,uiPassType,PUserConstantstring);
	VSRenderer::ms_pRenderer->CreatePInputDeclare(MSPara1,uiPassType,PInputDeclare);
	VSRenderer::ms_pRenderer->CreatePOutputDeclare(MSPara1,uiPassType,POutputDeclare);
	VSRenderer::ms_pRenderer->CreatePFuntion(MSPara1,uiPassType,PFuntionString);
	if (uiPassType == VSPass::PT_MATERIAL)
	{
		
	
		
		PShaderTextName = VSResourceManager::ms_OutputShaderCodePath + pMaterial->GetShowName().GetString() + ShaderID +_T("PShader.txt");


		
	}
	else if (uiPassType == VSPass::PT_NORMALDEPTH)
	{

		PShaderTextName = VSResourceManager::ms_OutputShaderCodePath + pMaterial->GetShowName().GetString() + _T("_NormalDeph") + ShaderID + _T("PShader.txt");

	}
	else if (uiPassType == VSPass::PT_PREZ)
	{


		PShaderTextName = VSResourceManager::ms_OutputShaderCodePath + pMaterial->GetShowName().GetString() + _T("_PreZ") + ShaderID +_T("PShader.txt");

	}
	PShaderText.Open(PShaderTextName.GetBuffer());
	PShaderString = PInclude + PDynamic + PUserConstantstring + PInputDeclare + POutputDeclare + PFuntionString;
	PShaderText.WriteInfo(PShaderString.GetBuffer());
#ifdef _DEBUG
	OutShaderFileName = PShaderTextName;
#endif
	return 1;
}




